//弹窗信息内容
import React from 'react';
import { connect } from 'dva';
import { FormGroup, DATA_ENTRY, codeType } from '../../../../comps';
import moment from 'moment';
import { formSelectData } from '../../../../utils/utils';
class MoneyLayer extends React.Component {
  constructor() {
    super();
    this.state = {
      tradeContent: null, //证券代码的选择内容
    };
  }
  componentDidMount() {
    this.props.onRef && this.props.onRef(this);
    if (this.props.type == 'edit' && this.props.LayerContent) {
      let info = this.props.LayerContent;
      this.setState({
        startDate: info.startDate ? moment(info.startDate) : '',
        endDate: info.endDate ? moment(info.endDate) : '',
      });
      if (!info.area)
        this.formContent?.props.form.setFieldsValue({
          area: info.financeLocationProvince == info.financeLocationCity ? info.financeLocationCity || '' : (info.financeLocationProvince || '') + (info.financeLocationCity || ''),
        });
    }
  }
  //选择证券代码
  changeData = (data) => {
    if (this.props.type == 'edit') {
      return false;
    }
    this.setState(
      {
        tradeContent: data,
      },
      () => {
        //此时是下拉选择的是
        if (data?.tradingCode) {
          this.props.dispatch({
            type: 'manMoneyDirectFinancialM/getTradeDetail',
            payload: {
              exchangeCode: data?.exchangeCode,
              tradingCode: data?.tradingCode,
            },
            callback: (res) => {
              if (res) {
                this.formContent.props.form.setFieldsValue({
                  issuerIndustry: res?.issuerIndustry,
                  issuerScale: res?.issuerScale,
                  financeLocationProvince: res?.financeLocationProvince,
                  financeLocationCity: res?.financeLocationCity,
                  interestRate: res?.interestRate ? res.interestRate : res?.interestRate == 0 ? 0 : undefined,
                });
                if (res?.startDate) this.formContent.props.form.setFieldsValue({ startDate: moment(res?.startDate) });
                if (res?.endDate) this.formContent.props.form.setFieldsValue({ endDate: moment(res?.startDate) });
                this.formContent.props.form.setFieldsValue({
                  area:
                    res?.financeLocationProvince == res?.financeLocationCity
                      ? res?.financeLocationCity || ''
                      : (res?.financeLocationProvince || '') + (res?.financeLocationCity || ''),
                });
              }
            },
          });
        }
      },
    );
  };
  getFormData = (callback) => {
    const { tradeContent } = this.state;
    this.formContent.props.form.validateFields((error, values) => {
      if (error) {
        return;
      }
      values.startDate = values.startDate ? values.startDate.format('YYYY-MM-DD') : undefined;
      values.endDate = values.endDate ? values.endDate.format('YYYY-MM-DD') : undefined;
      values.interestRate = parseFloat(values.interestRate / 100).toFixed(5) * 1;
      callback({
        ...values,
        tradeContent,
      });
    });
  };
  //日期变更 到期日不可小于起息日
  dateChange = (e, type) => {
    this.setState({
      [type]: e,
    });
  };
  //清空表单数据
  reset = () => {
    const {
      form: { resetFields },
    } = this.formContent.props;
    resetFields();
  };

  render() {
    const { type, LayerContent, borrowerCodeList } = this.props;
    const {
      globalM: {
        dataDictionary: { LOAN_DIRECTION_DICT = [] },
      },
    } = this.props;
    return (
      <div>
        <FormGroup
          widthSet={1000}
          dataSource={[
            {
              dataIndex: 'tradingCode',
              title: '证券代码',
              codeType: codeType.lczr,
              type: DATA_ENTRY.SECURITIESCODE,
              disabled: type == 'edit' ? true : false,
              rules: [
                {
                  required: true,
                  message: '请选择证券代码',
                },
              ],
              span: 12,
              initialValue: LayerContent?.tradingCode ? LayerContent?.exchangeCode + '-' + LayerContent?.tradingCode : undefined,
              isInput: true,
              onChange: this.changeData,
              positionBody: true,
              onRef: (ref) => (this.formContent = ref),
            },
            {
              dataIndex: 'issuerIndustry',
              title: '融资企业所属行业',
              type: DATA_ENTRY.FUNDAPPLICATIONINDUSTRYC,
              OnlySupportFirstLevel: true,
              parentCanClick: true,
              initialValue: LayerContent?.issuerIndustry,
              positionBody: true,
              rules: [{ required: true, message: '请选择' }],
              span: 12,
            },
            {
              type: DATA_ENTRY.ADDRESSC,
              provinceDataIndex: 'financeLocationProvince',
              cityDataIndex: 'financeLocationCity',
              provInit: LayerContent?.financeLocationProvince,
              provinceDataTitle: '融资企业所在省',
              cityDataTitle: '融资企业所在市',
              allowClear: true,
              cityInit: LayerContent?.financeLocationCity,
              disabled: false,
              positionBody: true,
              span: 24,
              rules: [
                [
                  {
                    required: true,
                    message: '请选择',
                  },
                ],
              ],
              onChange: (e) => {
                if (e && this.formContent)
                  this.formContent.props.form.setFieldsValue({
                    area: e.province == e.city ? e.city || '' : (e.province || '') + (e.city || ''),
                  });
              },
            },
            {
              dataIndex: 'issuerScale',
              title: '发行机构类型（按规模划分）',
              type: DATA_ENTRY.SELECT,
              initialValue: LayerContent?.issuerScale,
              dataSource: this.props.manMoneyDirectFinancialM?.dataDictionary?.ISSUER_BY_SIZE?.map((item) => {
                let obj = {
                  title: item.value,
                  value: item.code,
                };
                return obj;
              }),
              disabled: false,
              span: 12,
              positionBody: true,
              rules: [{ required: true, message: '请选择发行机构类型（按规模划分）!' }],
            },
            {
              dataIndex: 'loanGuaranteeType',
              title: '贷款担保方式',
              type: DATA_ENTRY.INPUT,
              initialValue: LayerContent?.loanGuaranteeType || 'D',
              disabled: false,
              span: 12,
              helper: 'D – 信用/免担保贷款',
              rules: [{ required: true, message: '请输入贷款担保方式!' }],
            },
            {
              dataIndex: 'area',
              title: '地区',
              type: DATA_ENTRY.INPUT,
              initialValue: LayerContent?.area,
              disabled: false,
              span: 12,
            },
            {
              dataIndex: 'areaCode',
              title: '地区代码',
              type: DATA_ENTRY.INPUT,
              initialValue: LayerContent?.areaCode,
              disabled: false,
              span: 12,
              rules: [{ required: true, message: '请输入地区代码!' }],
            },
            {
              dataIndex: 'borrowerCode',
              title: '借款人代码',
              type: DATA_ENTRY.INPUT,
              initialValue: LayerContent?.borrowerCode,
              disabled: false,
              span: 12,
              rules: [{ required: true, message: '请输入借款人代码!' }],
            },
            {
              dataIndex: 'borrowerCodeType',
              title: '借款人类型',
              type: DATA_ENTRY.INPUT,
              initialValue: LayerContent?.borrowerCodeType || '3',
              disabled: false,
              span: 12,
              helper: (
                <div>
                  {borrowerCodeList.map((item, key) => {
                    return <div key={key}>{item}</div>;
                  })}
                </div>
              ),
              rules: [{ required: true, message: '请输入借款人类型!' }],
            },
            {
              dataIndex: 'interestRate',
              title: '利率',
              type: DATA_ENTRY.INPUTNUMBER,
              initialValue: LayerContent?.interestRate ? parseFloat(LayerContent.interestRate * 100).toFixed(2) : null,
              disabled: false,
              unit: '%',
              precision: 2,
              span: 12,
              rules: [{ required: true, message: '请输入利率!' }],
            },
            {
              dataIndex: 'startDate',
              title: '起息日',
              type: DATA_ENTRY.SINGLEDATE,
              initialValue: LayerContent?.startDate ? moment(LayerContent?.startDate) : undefined,
              disabled: false,
              disableEnd: this.state.endDate || '',
              onChange: (e) => {
                this.dateChange(e, 'startDate');
              },
              span: 12,
              rules: [{ required: true, message: '请选择起息日!' }],
            },
            {
              dataIndex: 'endDate',
              title: '到期日',
              type: DATA_ENTRY.SINGLEDATE,
              initialValue: LayerContent?.endDate ? moment(LayerContent?.endDate) : undefined,
              disableBegin: this.state.startDate || '',
              onChange: (e) => {
                this.dateChange(e, 'endDate');
              },
              disabled: false,
              span: 12,
              rules: [{ required: true, message: '请选择到期日!' }],
            },
            {
              dataIndex: 'loanProductType',
              title: '贷款产品类别',
              type: DATA_ENTRY.INPUT,
              initialValue: LayerContent?.loanProductType || 'F99',
              disabled: false,
              span: 12,
              helper: 'F99 – 其他贷款',
              rules: [{ required: true, message: '请输入贷款产品类别!' }],
            },
            {
              dataIndex: 'loanDirection',
              title: '贷款实际投向',
              type: DATA_ENTRY.SELECT,
              initialValue: LayerContent?.loanDirection || 'E48',
              initialValue: LayerContent?.loanDirection || 'E48',
              dataSource: formSelectData(LOAN_DIRECTION_DICT).map((item) => {
                item.title = item.value + '-' + item.title;
                return item;
              }),
              positionBody: true,
              disabled: false,
              span: 12,
              rules: [{ required: true, message: '请选择贷款实际投向!' }],
            },
            {
              dataIndex: 'interestRateType',
              title: '利率是否固定',
              type: DATA_ENTRY.INPUT,
              initialValue: LayerContent?.interestRateType || 'RF01',
              disabled: false,
              span: 12,
              helper: (
                <div>
                  RF01 – 固定利率
                  <br />
                  RF02 – 其他
                </div>
              ),
              rules: [{ required: true, message: '请输入利率是否固定!' }],
            },
            {
              dataIndex: 'loanType',
              title: '贷款种类',
              type: DATA_ENTRY.INPUT,
              initialValue: LayerContent?.loanType || '3',
              disabled: false,
              helper: '3 – 其他债权',
              span: 12,
              rules: [{ required: true, message: '请输入贷款种类!' }],
            },
          ]}
          autoLayout={false}
        ></FormGroup>
      </div>
    );
  }
}
export default connect(({ manMoneyDirectFinancialM, globalM }) => ({ manMoneyDirectFinancialM, globalM }))(MoneyLayer);
